import router from '@/router'
import { useUserStore } from '@/stores/user'

// 白名单
const WHITE_ROUTE_LIST = ['/login']

/**
 * 路由前置守卫
 */
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore()

  if (userStore.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // 无 token 可进入白名单
    if (WHITE_ROUTE_LIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
